/**
 * 菜单JS
 */
angular.module('entrance.module.homepage', [
])

.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
	$stateProvider
		//首页
		.state('homepage', {
			url: '/homepage',
			templateUrl:'view/home/homepage.html',
			controller: 'HomepageController'
		})
		.state('sure', {
			url: '/sure/:dlvid',
			templateUrl:'view/home/sure.html',
			controller: 'SureController'
		})
		.state('courier', {
			url: '/courier/:dlvid',
			templateUrl:'view/home/courier.html',
			controller: 'CourierController'
		})
		.state('order', {
			url: '/order',
			templateUrl:'view/home/order.html',
			controller: 'OrderController'
		})
	;
}])
	.controller('HomepageController', ['$scope','$state','EntAuthService',function($scope,$state,EntAuthService){
		$scope.userid = 3;
		$scope.splid = 123;
		var courier = {
			zhoupeng:1001,
			diaosibin:1002,
			zhu:1003
		}
		$scope.courierid = courier.diaosibin;
		$scope.finish = function(){
			siw();
		}
		if($scope.userid == 1){
			EntAuthService.getDeliverybySplid($scope.splid).$promise.then(function(data){
				for(var i = 0;i<data.length;i++){
					data[i].deliveryGoods = [];
					data[i].deliveryStatus = [];
				}
				$scope.deliverys = data;
			});
		}else if($scope.userid == 3){
			EntAuthService.getDeliverybyCourierid($scope.courierid).$promise.then(function(data){
				for(var i = 0;i<data.length;i++){
					data[i].deliveryGoods = [];
					data[i].deliveryStatus = [];
				}
				$scope.deliverys = data;
			});
		}
		$scope.pushGoodAndStatus = function(delivery){
			var totalprice = 0;
			if(delivery.deliveryGoods.length==0 || delivery.deliveryStatus.length == 0){
				EntAuthService.getDelivery_goodsbyid(delivery.dlvid).$promise.then(function(data){
					for(var i=0;i<data.length;i++){
						totalprice+=data[i].goodsQuantity*data[i].goodsPrice;
					}
					data.totalprice = totalprice;
					delivery.deliveryGoods = data;
					EntAuthService.getDelivery_Statusbyid(delivery.dlvid).$promise.then(function(data){
						delivery.deliveryStatus = data;
						delvery_status(delivery.deliveryStatus,"#progressBar"+delivery.dlvid)
					});
				});
			}
		}
		$scope.takeOrder = function(dlivery){
			if(dlivery.deliveryStatus.length==1){
				$(".submit-ask-content").attr("disabled","true");
				var orderStatus = {
					dlvid:dlivery.dlvid,
					updatetime:"",
					status:1,
					statusDesc:'已接单',
					courierid:'',
				}
				EntAuthService.updataDelivery_Status(orderStatus,function(){
					$state.go("sure",{dlvid:orderStatus.dlvid})
					$(".submit-ask-content").removeAttr("disabled");
				},function(){
					$(".submit-ask-content").removeAttr("disabled");
				})
			}else{
				alert("该单子已经确认！！！")
			}
		}
		$scope.courierTakeOrder = function(delivery){
			if(delivery.deliveryStatus.length==2){
				$(".confirm-bton2").attr("disabled","true");
				var orderStatus = {
					dlvid:delivery.dlvid,
					updatetime:"",
					status:2,
					statusDesc:'配送中',
					courierid:$scope.courierid,
				}
				EntAuthService.updataDelivery_Status(orderStatus,function(){
					delivery.deliveryStatus.length = 3;
					$(".confirm-bton2").removeAttr("disabled");
				},function(){
					$(".confirm-bton2").removeAttr("disabled");
				})
			}else if(delivery.deliveryStatus.length>2){
				alert("该单子已被其他人派送！！！")
			}
		}
		$scope.sureService = function(delivery){
			if(delivery.deliveryStatus.length==3){
				$(".submit-ask-content").attr("disabled","true");
				var orderStatus = {
					dlvid:delivery.dlvid,
					updatetime:"",
					status:3,
					statusDesc:'已送达',
					courierid:$scope.courierid,
				}
				EntAuthService.updataDelivery_Status(orderStatus,function(){
					delivery.deliveryStatus.length = 4;
					$(".submit-ask-content").removeAttr("disabled");
				},function(){
					$(".submit-ask-content").removeAttr("disabled");
				})
			}else{
				alert("错误")
			}

		};
		$scope.checkCourier=function(delivery){
			$state.go("courier",{dlvid:delivery.dlvid})
		};
		$scope.courierCancelOrder = function(delivery,deliverys){
			var update = {
				dlvid:delivery.dlvid,
				courierid:0
			}
			EntAuthService.updataDeliver(update,function(){
				var indextodelete = -1;
				for(var i=0;i<deliverys.length;i++){
					if (deliverys[i].dlvid == delivery.dlvid) {
						indextodelete = i;
						break;
				    }
				}
				if (indextodelete >= 0) {
					deliverys.splice(indextodelete, 1);
				}
			})
		}
	}])
.controller('SureController', ['$scope','$state','$stateParams','EntAuthService',function($scope,$state,$stateParams,EntAuthService){
		$scope.checkCourier=function(){
			$state.go("courier",{dlvid:$stateParams.dlvid})
		}
	}])
.controller('CourierController', ['$scope','$state','$stateParams','EntAuthService',function($scope,$state,$stateParams,EntAuthService){
		$scope.dlvid = $stateParams.dlvid;
		console.log($stateParams.dlvid)
		EntAuthService.getCourier().$promise.then(function(data){
			$scope.Courier=data;
		});
		var delivery = {
			dlvid:$scope.dlvid,
			courierid:''
		}
		$scope.sureCourier = function(){
			delivery.courierid = $('input:radio:checked').val();
			EntAuthService.updataDeliver(delivery,function(){
				//
				$state.go("homepage")
			})
		}
	}])
.controller('OrderController', ['$scope','EntAuthService',function($scope,EntAuthService){
		EntAuthService.getDelivery().$promise.then(function(data){
			$scope.orderdeliverys = data;
				EntAuthService.getDelivery_goods().$promise.then(function(data){
					$scope.delivery_goods = data;
					for(var i=0;i<$scope.orderdeliverys.length;i++){
						$scope.orderdeliverys[i].goods = [];
						var totalprice = 0;
						for(var j=0;j<data.length;j++){
							if($scope.orderdeliverys[i].dlvid == data[j].dlvid){
								$scope.orderdeliverys[i].goods.push(data[j]);
								totalprice +=data[j].goodsQuantity*data[j].goodsPrice;
							}
							$scope.orderdeliverys[i].totalAmount = totalprice;
							$scope.orderdeliverys[i].length = $scope.orderdeliverys[i].goods.length;
						}

						console.log($scope.orderdeliverys[i])
		        }
			});
		});
		$scope.sureTake = function(){

		}
	}])
;
